package com.simple.accounting.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.simple.accounting.R;
import com.simple.accounting.db.UserDBHelper;
import com.simple.accounting.utils.ToastUtils;

import java.util.Calendar;

public class LoginActivity extends BaseActivity {


    /*设置多次点击的最小间隔*/
    public static final int MIN_CLICK_DELAY_TIME = 1000;
    /*上次点击时间*/
    private long lastClickTime = 0;

    private EditText edt_name;
    private EditText edt_pw;
    private CheckBox cb_rm;
    private ImageView show1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        showInfo();
        String name = getIntent().getStringExtra("name");
        edt_name.setText(name);
        String pwd = getIntent().getStringExtra("pwd");
        edt_pw.setText(pwd);
    }

    private void showInfo() {
        Boolean isChecked = sp.getBoolean("isChecked",false);
        if(isChecked){
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }
    }


    private void initView() {
        edt_name = (EditText) findViewById(R.id.edt_name);
        edt_pw = (EditText) findViewById(R.id.edt_pwd);
        cb_rm= (CheckBox) findViewById(R.id.cb_rm);
        Button loginBtn = findViewById(R.id.login);
        Button registryBtn = findViewById(R.id.register);
        show1 = findViewById(R.id.show1);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*防止两次快速点击照成login响应两次*/
                long currentTime = Calendar.getInstance().getTimeInMillis();
                if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME){
                    lastClickTime  = currentTime;
                    login();
                }
            }
        });
        registryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long currentTime = Calendar.getInstance().getTimeInMillis();
                if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME){
                    lastClickTime  = currentTime;
                    registry();
                }

            }
        });

        show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleShow();
            }
        });
    }

    boolean isShow = true;
    private void toggleShow() {
        if (isShow){
            edt_pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
            int len = edt_pw.getText().toString().trim().length();
            edt_pw.setSelection(len);
            show1.setImageResource(R.mipmap.hide);
            isShow = false;   //设置标志位为隐藏状态
        } else {  //密文---》明文
            edt_pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            int len = edt_pw.getText().toString().trim().length();
            edt_pw.setSelection(len);
            show1.setImageResource(R.mipmap.show);
            isShow = true;   //设置标志位为隐藏状态
        }
    }

    void registry() {
        startActivity(new Intent(LoginActivity.this,RegistryActivity.class));
    }
    void login(){
        String rm = cb_rm.getText().toString().trim();
        String name = edt_name.getText().toString().trim();
        String pwd = edt_pw.getText().toString().trim();//控件，取文本
        if (name.isEmpty()){
            ToastUtils.show(this,"输入的用户名不能为空！");
            return;
        }
        if(pwd.isEmpty()){
            ToastUtils.show(this,"输入的密码不能为空！");
            return;
        }
        boolean isChecked = cb_rm.isChecked();
        saveUserInfoWithSP(name, pwd,isChecked);
        UserDBHelper userDBHelper = new UserDBHelper(this);
        SQLiteDatabase database = userDBHelper.getReadableDatabase();
        Cursor cursor = database.query ("userinfo",null,null,
                null, null,null,null);
        while(cursor.moveToNext()){
            String dbName = cursor.getString(cursor.getColumnIndex("username"));
            String dbPwd = cursor.getString(cursor.getColumnIndex("password"));
            if(dbName.equals(name) && dbPwd.equals(pwd)){
                ToastUtils.showLongToast(this,"登录成功");
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
                return;
            }
        }

        ToastUtils.show(this,"用户名或密码有误！",ToastUtils.TYPE_ERROR);

    }

    private void saveUserInfoWithSP(String username,String pw,Boolean isChecked) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isChecked",isChecked);//editor是对map进行封装 限制用户输入的类型
        editor.putString("username",username);
        editor.putString("pw",pw);
        editor.commit();//提交commit
    }
}
